'''xpath_案例1解析百度一下
    解析网址： http://www.baidu.com
    需求：解析百度网页 获取“百度一下” 字体。'''

import urllib.request

baidu_url = 'http://www.baidu.com'
baidu_headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
}
request = urllib.request.Request(url=baidu_url, headers=baidu_headers)
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
# print(content)


# 使用xpath 解析网页
from lxml import etree

html_parse = etree.HTML(content)
res = html_parse.xpath('//input[@id="su"]/@value')[0]
print(res)